package com.bobo.medicinechest.dialog;

import android.content.Context;
import android.os.CountDownTimer;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.bobo.medicinechest.R;
import com.bobo.medicinechest.adapter.ShopCarAdapter;
import com.bobo.medicinechest.bean.GoodsBean;
import com.bobo.medicinechest.bean.GoodsTypeBean;
import com.bobo.medicinechest.databinding.DialogDrugDetailsBinding;
import com.bobo.medicinechest.databinding.DialogShopCarBinding;
import com.bobo.medicinechest.manager.CartManager;
import com.bobo.medicinechest.util.PriceFormatUtil;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.xuexiang.xui.widget.divider.CustomColorItemDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 药品详情Dialog
 * liumeng
 * 2025.11.20
 */
public class DrugDetailsDialog extends CenterPopupView {

    private Context mContext;
    private List<GoodsBean> list;
    private DialogDrugDetailsBinding binding;
    private List<GoodsBean> goodsListBeanList = new ArrayList<>();
    private CountDownTimer countDownTimer;
    private int mShopCarNumber = 1;

    public DrugDetailsDialog(@NonNull Context context, List<GoodsBean> list) {
        super(context);
        this.mContext = context;
        this.list = list;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_drug_details;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = DialogDrugDetailsBinding.bind(getPopupImplView());


        //初始化倒计时
        initCountDownTimer();

        binding.tvBack.setOnClickListener(v -> {
            countDownTimer.cancel();
            dismiss();
        });
        binding.ivMinus.setOnClickListener(v -> {
            if (mShopCarNumber > 1) {
                mShopCarNumber--;
            } else if (mShopCarNumber == 1) {
                ToastUtils.showShort("");
            }

        });
        binding.ivAdd.setOnClickListener(v -> {
            mShopCarNumber++;
            binding.tvPay.setText("加入购物车（共"+ CartManager.getInstance().getTotalCount()+ "件");
            binding.tvPrice.setText(PriceFormatUtil.formatPrice(CartManager.getInstance().getTotalPrice(), 12, 21, 13));
        });
        binding.llDrugInstructions.setOnClickListener(v -> {
            //药品说明书

        });
        binding.tvPay.setOnClickListener(v -> {

        });


    }

    private void initCountDownTimer() {
        // 创建倒计时器
        countDownTimer = new CountDownTimer(300000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // 每秒更新一次文本
                int seconds = (int) (millisUntilFinished / 1000);
                binding.tvBack.setText(String.format(Locale.getDefault(), "返回(%ds)", seconds));
            }

            @Override
            public void onFinish() {
                binding.tvBack.setText("返回(0s)");
                dismiss();
            }
        };
        countDownTimer.start();
    }

    @Override
    protected int getMaxHeight() {
        return (int) Math.round(XPopupUtils.getScreenHeight(mContext) * 0.7);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }
}
